package com.apes.sms.systemSetting.model;

import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.scm.masterdata.product.model.Classify;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lishikun on 2021/12/17 11:10
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = ClassifyBusiness.CONST_TABLE_NAME)
public class ClassifyBusiness extends BaseModel {

    public static final String CONST_TABLE_NAME = "smsr_classify_business";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "供应商分类")
    @Comment("编码")
    private String id;

    @ManyToOne(optional = false)
    @JoinColumn(name = "scope_business_id", referencedColumnName = "id")
    @Comment("经营范围编码")
    private ScopeBusiness scopeBusiness;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "classify_id", referencedColumnName = "id")
    @Comment("品类")
    private Classify classify;

    @ManyToMany(cascade = {CascadeType.DETACH}, fetch = FetchType.LAZY)
    @JoinTable(name = "sms_exclude_classify",
            joinColumns = {@JoinColumn(name = "classify_business_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "classify_id", referencedColumnName = "id")}
    )
    private List<Classify> excludeClassify = new ArrayList<>();

    // 以下字段给前端临时展示用
    @Transient
    private int levels;

    public int getLevels() {
        return levels = this.classify.getLevels();
    }
}
